package com.tanhua.sso.service;

import com.baidu.aip.face.AipFace;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;

@Service
public class FaceEngineService {
    //设置APPID/AK/SK
    @Value("${baiDu.appId}")
    private String APP_ID ;
    @Value("${baiDu.apiKey}")
    private String API_KEY ;
    @Value("${baiDu.secretKey}")
    private String SECRET_KEY;

    private AipFace client;
    public boolean checkIsPortrait(String image){
        // 调用接口
        // 人脸检测
        String imageType = "URL";
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");
        JSONObject res = client.detect(image,imageType,options);
        try {
            System.out.println(res.toString(2));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return res.get("error_msg") == "SUCCESS" ? true : false;

    }



    @PostConstruct
    public void init() {
        // 初始化一个AipFace
        AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        this.client = client;

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
//        client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
//        client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

//        // 调用接口
//        String image = "取决于image_type参数，传入BASE64字符串或URL字符串或FACE_TOKEN字符串";
//        String imageType = "BASE64";
//
//        // 人脸检测
//        JSONObject res = client.detect(image, imageType, options);
//        System.out.println(res.toString(2));

    }
}